package pdfsetter.core.schema.selectors;

import java.util.Iterator;

import pdfsetter.core.schema.advanced.FilePageSelection;
import pdfsetter.core.schema.advanced.FilePageSelector;
import pdfsetter.core.util.RandomAccessIterableIterator;

/**
 * Another basic selector. We duplicate each page with pageMultiplier.
 * 
 * @author Andrzej Dorobisz
 */
public class MultiplyingPageSelector implements FilePageSelector {
    private final int pageMultiplier;

    public MultiplyingPageSelector(int pageMultiplier) {
        this.pageMultiplier = pageMultiplier;
    }

    @Override
    public FilePageSelection selectPages(int fileSize) {
        return new MultipliedPageSelection(fileSize);
    }

    private class MultipliedPageSelection implements FilePageSelection {
        private final int size;

        public MultipliedPageSelection(int fileSize) {
            size = fileSize * pageMultiplier;
        }

        @Override
        public Integer get(int index) {
            if (index >= size || index < 0)
                throw new IndexOutOfBoundsException();
            return index / pageMultiplier;
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public Iterator<Integer> iterator() {
            return new RandomAccessIterableIterator<Integer>(this);
        }
    }
}
